package com.zjitc.utils;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * Create By IntelliJ IDEA
 *
 * @author: jsonor
 * @create-Time: 2017/10/30 15:59
 * @description: ${description}
 */
public class Utils {

  /**
   * 找寻某个包路径下的所有的类描述文件
   *
   * @param packName 包路径(com.zjitc.control)
   * @return 返回所有的类描述文件
   */
  public static List<Class> getAllClassFromPackage(String packName) throws ClassNotFoundException {
    List<Class> clas = new ArrayList<Class>();
    String path = packName.replace('.', '/');
    // 查找class目录下对应的文件名即可
    URL url = Thread.currentThread().getContextClassLoader().getResource(path);

    // 该路径下的所有文件的名字
    File file = new File(url.getPath());

    File[] files = file.listFiles();
    for (File childrenFile : files) {
      String fileName = childrenFile.getName();
      String className = fileName.substring(0, fileName.length() - 6);
      String fullClass = packName + "." + className;
      clas.add(Class.forName(fullClass));
    }

    return clas;
  }
}
